﻿/*
 * ServiceHandler
 * 
 * Author: snowdreamist
 * Date: 2011/11/24
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;
using BitTorrentSharp.KRPC.Messages;
using BitTorrentSharp.KRPC.Contract;
using BitTorrentSharp.KRPC.Threading;

namespace BitTorrentSharp.KRPC.Handler
{
    public class InvokeHandler
    {
        /// <summary>
        /// Invoke a remote method
        /// </summary>
        /// <param name="message">method call from proxy</param>
        /// <returns>result</returns>
        public object InvokeRemoteMethod(MethodCall message)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Invoke a local method
        /// </summary>
        /// <param name="context">message context</param>
        /// <returns>result</returns>
        public void InvokeLocalMethod(MessageContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            object r = null;

            try
            {
                r = context.Operation.Invoke(context);
            }
            catch (Exception error)
            {
                context.Error = error;
            }

            //context.Results = r;
        }
        /// <summary>
        /// Resume a blocked local method
        /// </summary>
        /// <param name="context">message context</param>
        public void ResumeLocalMethod(MessageContext context, KRPCAsyncInvokeContext invokeContext)
        {
            // resume
            context.Operation.Resume(context, invokeContext);
        }
    }
}
